package com.guigu.pd.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.guigu.pd.base.entity.TruckDriverLicenseEntity;
import com.guigu.pd.base.service.TruckDriverLicenseService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.PageVo;

import com.guigu.pd.base.entity.FleetEntity;
import com.guigu.pd.base.service.FleetService;
import com.guigu.pd.serviceapi.authfeign.CoreOrgFeign;
import com.guigu.pd.serviceapi.authfeign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.guigu.pd.base.dao.TruckDriverDao;
import com.guigu.pd.base.entity.TruckDriverEntity;
import com.guigu.pd.base.service.TruckDriverService;

import javax.swing.*;


@Service("truckDriverService")
public class TruckDriverServiceImpl extends ServiceImpl<TruckDriverDao, TruckDriverEntity> implements TruckDriverService {

    @Autowired
    private UserFeign userFeign;




    @Autowired
    private CoreOrgFeign coreOrgFeign;

    @Autowired
    private FleetService fleetService;

    @Autowired
    private TruckDriverLicenseService truckDriverLicenseService;


    @Override
    public PageVo<TruckDriverEntity> queryTruckDriverfy(Integer pageNum, Integer pagesize,
                                                        LambdaQueryWrapper<TruckDriverEntity> queryWrapper) {


        PageHelper.startPage(pageNum, pagesize);

        List<TruckDriverEntity> list = this.list(queryWrapper);

        for (TruckDriverEntity truckDriverEntity : list) {

                 //获取司机名称
            String getnamebyid = userFeign.getnamebyid(truckDriverEntity.getUserId());


            String[] str =  getnamebyid.split(",");

            //获取司机所在机构名称
            String getcornamebyid = coreOrgFeign.getcornamebyid(str[1]);

            //根据车队编号查询车队名称
            FleetEntity byId = fleetService.getById(truckDriverEntity.getFleetId());

            truckDriverEntity.setName(str[0]);
            truckDriverEntity.setCoreOrgname(getcornamebyid);
            truckDriverEntity.setFleetname(byId.getName());
        }

        PageInfo<TruckDriverEntity> pageInfo = new PageInfo<>(list);

        PageVo<TruckDriverEntity> pageVo = new PageVo<>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public TruckDriverEntity getcarInfobyId(String id) {
        TruckDriverEntity car = this.getById(id);
        TruckDriverLicenseEntity truckDriverLicense = truckDriverLicenseService.getcarInfobyId(car.getUserId());
        car.setTruckDriverLicense(truckDriverLicense);

        //获取司机名称
        String getnamebyid = userFeign.getnamebyid(car.getUserId());

        String[] str =  getnamebyid.split(",");

        //获取司机所在机构名称
        String getcornamebyid = coreOrgFeign.getcornamebyid(str[1]);

        //根据车队编号查询车队名称
        FleetEntity byId = fleetService.getById(car.getFleetId());

        car.setName(str[0]);
        car.setCoreOrgname(getcornamebyid);
        car.setFleetname(byId.getName());

        return car;
    }

}
